from qturboframe.lib.utils.by import By
from qturboframe.lib.ranorex.win_control.control import Control

from qturboframe.public_ui.ucf_wpf.button import Button
from qturboframe.public_ui.ucf_wpf.input_type import InputType
from qturboframe.public_ui.ucf_wpf.tab_page import TabPage
from qturboframe.public_ui.ucf_wpf.checkbox import CheckBox
from qturboframe.public_ui.ucf_wpf.radio_button import RadioButton
from qturboframe.public_ui.ucf_wpf.message_box import MessageBox
from qturboframe.public_ui.ucf_wpf.combobox import Combobox
from qturboframe.public_ui.ucf_wpf.table import Table
from qturboframe.public_ui.ucf_wpf.date_time import DateTime
from qturboframe.public_ui.ucf_wpf.tree import Tree

class FrontEnd(object):
    """
    ucf wpf类库的入口类

    :Attributes:
     - driver: 控件所在的当前Control对象
     - timeout: 步骤操作的超时时间

    :Example:
     - 例1: FrontEnd(control)
    """

    def __init__(self, driver: Control, timeout=40000):
        self.driver = driver
        self.timeout = timeout

    def button(self, idname, father=None, timeout=None):
        '''
        获取ucf wpf的按钮对象

        :Args:
         - idname: 按钮的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 按钮的祖先级元素，可以给按钮查找限定范围，不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf wpf的按钮对象

        :Example:
         - 例1: button = FrontEnd(driver).button(idname = {By.TEXT: '导入'})
         - 例2: button = FrontEnd(driver).button(idname = {By.TEXT: '导入'}, father=msg_box)
         - 例3: button = FrontEnd(driver).button(control)
        '''
        if not father:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        return Button(idname, father=father, timeout=timeout)

    def input_type(self, idname=None, father=None, timeout=None):
        '''
        获取ucf wpf 输入框对象

        :Args:
         - idname: 输入框的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 输入框的祖先级元素，可以给输入框查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf wpf的输入框对象

        :Example:
         - 例1: input_type = FrontEnd(driver).input_type({By.TEXT: "Height(控件高度)"})
         - 例2: input_type = FrontEnd(driver).input_type({By.TEXT: "Height(控件高度)"}, father)
        '''
        if not father:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        return InputType(idname, father, timeout)

    def combobox(self, idname=None, father=None, index=1, timeout=None):
        '''
        获取ucf wpf 组合框对象

        :Args:
         - idname: 组合框的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 组合框的祖先级元素，可以给组合框查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf wpf的组合框对象

        :Example:
         - 例1: combobox = FrontEnd(driver).combobox({By.TEXT: "Height(控件高度)"})
         - 例2: combobox = FrontEnd(driver).combobox({By.TEXT: "Height(控件高度)"}, father)
        '''
        if father is None:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        return Combobox(idname, father, timeout, index=index)

    def checkbox(self, idname=None, father=None, timeout=None):
        '''
        获取ucf 多选框对象

        :Args:
         - idname: 多选框的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 多选框的祖先级元素，可以给多选框查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf多选框对象

        :Example:
         - 例1: checkbox = FrontEnd(driver).checkbox(father=form)
         - 例2: checkbox = FrontEnd(driver).checkbox({'id':'id1'})
        '''
        if not father:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        if idname:
           return CheckBox(idname, father, timeout)

        return CheckBox({By.XPATH: ".//checkbox"}, father, timeout)

    def radio_button(self, idname, father=None, timeout=None):
        '''
        获取ucf 单选按钮对象

        :Args:
         - idname: 单选按钮的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 单选按钮的祖先级元素，可以给单选按钮查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf单选按钮对象

        :Example:
         - 例1: input_type = FrontEnd(driver).select(father=form)
         - 例2: input_type = FrontEnd(driver).select({'id':'id1'})
        '''
        if not father:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        return RadioButton(idname, father, timeout)

    def message_box(self, idname, father=None, timeout=None):
        '''
        获取ucf wpf弹出信息框
        :Args:
         - idname: 弹出信息框的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 弹出信息框的祖先级元素，可以给弹出信息框查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf wpf弹出信息框

        :Example:
         - 例1: message_box = FrontEnd(driver).message_box(father=form)
         - 例2: message_box = FrontEnd(driver).message_box({'By.TEXT':'提示'})
        '''
        if not father:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        return MessageBox(idname, father, timeout)

    def tab_page(self, idname, father=None, timeout=None):
        '''
        获取ucf wpf Tab选项卡
        :Args:
         - idname: Tab选项卡的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: Tab选项卡的祖先级元素，可以给Tab选项卡查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf选项卡

        :Example:
         - 例1: input_type = FrontEnd(driver).tab(father=form)
         - 例2: input_type = FrontEnd(driver).tab({'By.TEXT':'买入[F1]'})
        '''
        if father is None:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        return TabPage(idname, father, timeout)

    def table(self, idname=None, father=None, timeout=None):
        '''
        获取ucf 表格
        :Args:
         - idname: 表格的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 表格的祖先级元素，可以给表格查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf表格

        :Example:
         - 例1: table = FrontEnd(driver).table(father=form)
         - 例2: table = FrontEnd(driver).table({'id':'id1'})
        '''
        if not father:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        if idname:
            return Table(idname, father, timeout)
        else:
            return Table({"xpath": ".//table"}, father, timeout)

    def date_time(self, idname=None, father=None, timeout=None):
        '''
        获取ucf 时间对象
        :Args:
         - idname: 时间对象的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 时间对象的祖先级元素，可以给时间对象查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf 时间对象

        :Example:
         - 例1: date_time = FrontEnd(driver).date_time(father=form)
         - 例2: date_time = FrontEnd(driver).date_time({'id':'id1'})
        '''
        if father is None:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        if idname:
            return DateTime(idname, father, timeout)
        else:
            return DateTime({"xpath": ".//datetime"}, father, timeout)

    def tree(self, idname=None, father=None, timeout=None):
        '''
        获取ucf wpf 树形控件
        :Args:
         - idname: 表格的定位条件，字典类型：{'元素查找方式':'元素查找语句', '辅助属性':'辅助属性值'}
         - father: 表格的祖先级元素，可以给表格查找限定范围，这样就不需要在整个页面中查找
         - timeout: 查找按钮的超时时间

        :Returns:
            ucf表格

        :Example:
         - 例1: tree = FrontEnd(driver).tree(father=form)
         - 例2: tree = FrontEnd(driver).tree({'id':'id1'})
        '''
        if not father:
            father = self.driver

        if not timeout:
            timeout = self.timeout

        if idname:
            return Tree(idname, father, timeout)
        else:
            return Tree({"xpath": ".//tree"}, father, timeout)

if __name__=="__main__":
    from qturboframe.lib.ranorex.client import CsClient
    import time

    # #need to test
    # #界面右上的搜索框
    # cs = CsClient()
    # main_frame = cs.find_control_by_path("/form[@title='QuantTool']")
    #
    # fe = FrontEnd(main_frame)
    # # fe.input_type(text='菜单/组件').set_value('行情')
    # fe.input_type(text='菜单/组件')
    # # fe.button(is_search_button=True).click()

    # #tab选项卡
    # cs = CsClient()
    # main_frame = cs.find_control_by_path("/form[@automationid='QTMainWindow']")
    #
    # fe = FrontEnd(main_frame)
    # fe.tab()

    # #可输入的下拉菜单
    # from qturboframe.lib.ranorex.client import CsClient
    #
    # cs = CsClient()
    # main_frame = cs.find_control_by_path("/form[@title='QuantTool']")
    # fe = FrontEnd(main_frame)
    # fe.select(text="菜单/组件").select_signle_value("盘后定价行情 - 组件")

    # #盘后定价行情Form
    # from qturboframe.lib.ranorex.client import CsClient
    # cs = CsClient()
    # main_frame = cs.find_control_by_path("/form[@title='QuantTool']")
    # fe = FrontEnd(main_frame)
    # fe.form(processname="QuantShell").select("证券代码").select_signle_value("125233")
    # fe.form(processname="QuantShell").button(text="查询").click()
    # fe.form(processname="QuantShell").get_record_count()

    # #期货交易
    # from qturboframe.lib.ranorex.client import CsClient
    # cs = CsClient()
    # main_frame = cs.find_control_by_path("/form[@title='QuantTool']")
    # fe = FrontEnd(main_frame)
    # form = fe.form(processname="QuantShell")
    # form.select(text="基金：").select_signle_value("测试普通基金55550003")
    # form.select(text="投资组合：").select_signle_value("11")
    # form.select(text="合约代码：").select_signle_value("a1209")
    # form.radio(text="买卖方向：").select_radio("卖出")
    # form.radio(text="开平方向：").select_radio("平仓")
    # form.select(text="指定价").set_number("2345")
    # form.select(text="委托数量：").set_number("2222")
    # form.button(text="下单").click()











